package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.entity.ImageViewpager;
import com.example.zbh.dsliao.utils.DliaoImageCache;
import com.example.zbh.dsliao.utils.DliaoUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 点击图片放大的viewpager适配器
 */
public class LargeImageViewPagerAdapter extends PagerAdapter implements View.OnClickListener, View.OnLongClickListener {
    
    private List<ImageViewpager> list;
    private Context context;
    
    public void addData(Context context, List<ImageViewpager> list){
        this.list = list;
        Log.d("gedadadada", "list:" + list + "<<<<<<<<<");
        this.context = context;
        notifyDataSetChanged();
    }


    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**
     * 返回当前窗体数
     * **/
    @Override
    public int getCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }

    /**
     * 初始化position的界面
     * **/
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.viewpager_largeimage_gut, null);
        ImageView affiche_image = (ImageView) view.findViewById(R.id.affiche_image);
        affiche_image.setOnClickListener(this);
        affiche_image.setOnLongClickListener(this);
        //设置图片到imageview上
        if (list != null && list.size() > 0){
            ImageViewpager viewpager = list.get(position);
            String avatar = viewpager.getImageUrl();
            setUserAvatar(context, avatar, affiche_image);
        }
        container.addView(view);
        return view;
    }


    /**
     * 设置图片到弹窗中显示
     * **/
    private void setUserAvatar(Context context, String avater, ImageView userImg) {
        if (avater != null){
            Glide.with(context).load(avater).placeholder(R.mipmap.dl_iconfont_jiazaishibai).into(userImg);
        }else {
            userImg.setImageResource(R.mipmap.dl_iconfont_jiazaishibai);
        }
    }
    
    /**
     * 判断是否由对象产生界面
     * **/
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }



    /**
     * 短按回调1
     * **/
    @Override
    public void onClick(View v) {
        onPageDismissClickListener.onDismissClick(1);
    }
    /**
     * 长按回调2
     * **/
    @Override
    public boolean onLongClick(View v) {
        onPageDismissClickListener.onDismissClick(2);
        return false;
    }


    private OnPageDismissClickListener onPageDismissClickListener;

    public void setOnPageDismissClickListener (OnPageDismissClickListener onPageDismissClickListener){
        this.onPageDismissClickListener = onPageDismissClickListener;
    }

    public interface OnPageDismissClickListener{

        void onDismissClick(int code);
    }





}
